{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-25T14:09:04.014839Z",
     "start_time": "2025-02-25T14:09:03.940636Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import random\n",
    "import time"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:10:57.404673Z",
     "start_time": "2025-02-25T14:10:40.950421Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a=[]\n",
    "for _ in range(10**8):\n",
    "    a.append(random.random()) # random.random()的作用是生成一个0-1之间的随机数\n",
    "print('-'*50)\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "\n",
    "b = np.array(a)\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "id": "bae33d547477ba9c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "0.7979452610015869 0.11281991004943848\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.1 创建一维数组",
   "id": "c8d9001f4e728498"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:12:03.057919Z",
     "start_time": "2025-02-25T14:12:03.051822Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list1 = [1,2,3,4]\n",
    "oneArray = np.array(list1) \n",
    "print(type(oneArray))  #对象类型\n",
    "print(list1)\n",
    "print(oneArray)\n",
    "\n",
    "# 创建数组的多种形式\n",
    "# 1. 直接传入列表的方式\n",
    "t1 = np.array([1,2,3]) \n",
    "print(t1) \n",
    "print(type(t1))\n",
    "'''\n",
    "[1 2 3]\n",
    "<class 'numpy.ndarray'> '''\n",
    "\n",
    "# 2. 传入range生成序列\n",
    "t2 = np.array(range(10)) \n",
    "print(t2) \n",
    "print(type(t2))\n",
    "'''\n",
    "[0 1 2 3 4 5 6 7 8 9]\n",
    "<class 'numpy.ndarray'> '''\n",
    "\n",
    "# 3. 使用numpy自带的np.arange()生成数组\n",
    "t3 = np.arange(0,10,2) \n",
    "print(t3) \n",
    "print(type(t3))"
   ],
   "id": "a2860d4ad3b9c7e5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1, 2, 3, 4]\n",
      "[1 2 3 4]\n",
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T06:58:28.082997Z",
     "start_time": "2025-02-25T06:58:28.080209Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2 = [[1,2],[3,4],[5,6]] # 生成二维数组，三行二列\n",
    "\n",
    "twoArray = np.array(list2) \n",
    "print(twoArray)"
   ],
   "id": "a6fc24afd23426e4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:00:55.621314Z",
     "start_time": "2025-02-25T07:00:55.618304Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取数组的维度( 注意： 与函数的参数很像) \n",
    "print(twoArray.ndim)\n",
    "\n",
    "# 形状（行，列）\n",
    "print(twoArray.shape)\n",
    "\n",
    "# 有多少个元素\n",
    "print(twoArray.size)"
   ],
   "id": "a2803f52330b6132",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.4 调整数组形状",
   "id": "969ed376da7979d6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:34:31.550301Z",
     "start_time": "2025-02-25T07:34:31.546301Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four = np.array([[1,2,3],[4,5,6]])\n",
    "\n",
    "# 修改的是原有的\n",
    "print(id(four))\n",
    "four.shape = (3,2) \n",
    "print(f'four={four},id(four)={id(four)}')\n",
    "\n",
    "# 返回一个新的数组\n",
    "four1 = four.reshape(3,2) # reshape()方法可以改变数组的形状,但不会改变原数组的值\n",
    "print(four1)\n",
    "print(id(four1))"
   ],
   "id": "9d28ebae9847e720",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2995710573328\n",
      "four=[[1 2]\n",
      " [3 4]\n",
      " [5 6]],id(four)=2995710573328\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "2995202587120\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:36:51.278337Z",
     "start_time": "2025-02-25T07:36:51.275337Z"
    }
   },
   "cell_type": "code",
   "source": [
    "five=four.reshape((6,))\n",
    "five"
   ],
   "id": "fe94aa0fb8f02f4a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:37:06.221237Z",
     "start_time": "2025-02-25T07:37:06.216731Z"
    }
   },
   "cell_type": "code",
   "source": [
    "six = four.flatten(order='C') # C的意思是按行优先，F的意思是按列优先\n",
    "six"
   ],
   "id": "3fc5c6c131f585ec",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:16:48.127840Z",
     "start_time": "2025-02-25T14:16:48.122906Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 拓展：数组的形状\n",
    "t = np.arange(24) \n",
    "print(t)\n",
    "print(t.shape)\n",
    "# 转换成二维\n",
    "t1 = t.reshape((2,3,4)) # 将t的形状改成2行3列4个元素，原数组的元素会被复制\n",
    "print(t1)\n",
    "print(t1.shape)\n",
    "t2=t1.reshape((24,))\n",
    "print(t2)"
   ],
   "id": "d89968874ec19d37",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "(2, 3, 4)\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.5 数组变列表",
   "id": "38f523009d97da7e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:17:55.075155Z",
     "start_time": "2025-02-25T14:17:53.952684Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将数组转成list\n",
    "a= np.array([9, 12, 88, 14, 25])\n",
    "list_a = a.tolist() \n",
    "list_a"
   ],
   "id": "f8009b4266738a13",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 12, 88, 14, 25]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 5 数据类型",
   "id": "eb703aa4654953e6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:49:02.863915Z",
     "start_time": "2025-02-25T07:49:02.860264Z"
    }
   },
   "cell_type": "code",
   "source": [
    "f = np.array([1,2,3,4,5],dtype=np.int16) \n",
    "print(f.itemsize) # 1 np.int8(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)\n",
    "\n",
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "\n",
    "# 拓展随机生成小数\n",
    "# 使用python语法，保留两位\n",
    "print(round(random.random(),2))\n",
    "\n",
    "arr = np.array([random.random() for i in range(10)],dtype=np.float16)\n",
    "# 取小数点后两位\n",
    "print(np.round(arr,2))\n",
    "print(arr.dtype)"
   ],
   "id": "d48bc9a484e84d91",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n",
      "int64\n",
      "0.07\n",
      "[0.98 0.95 0.41 0.02 0.96 0.34 0.37 0.68 0.64 0.12]\n",
      "float16\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6 数组的计算",
   "id": "33e010ffc2690a43"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 6.1 数组和数",
   "id": "6648878fce8a570d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:18:26.546949Z",
     "start_time": "2025-02-25T14:18:26.537082Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 =np.arange(24).reshape((6,4)) \n",
    "print(t1+2)\n",
    "print(\"-\"*20)\n",
    "print(t1*2) \n",
    "print(\"-\"*20)\n",
    "print(t1/2)"
   ],
   "id": "9d665fe8227d7d1e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:54:33.472867Z",
     "start_time": "2025-02-25T07:54:33.470361Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组与数组的计算\n",
    "t1 = np.arange(24).reshape((6,4))\n",
    "t2 = np.arange(100,124).reshape((6,4))\n",
    "\n",
    "print(t1+t2) \n",
    "print(\"-\"*20)\n",
    "print(t1-t2)"
   ],
   "id": "c458245ff6ea03f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "--------------------\n",
      "[[-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:20:08.854355Z",
     "start_time": "2025-02-25T14:20:08.848707Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#形状不一样的数组不能进行计算\n",
    "t1 = np.arange(24).reshape((4,6)) \n",
    "t2 = np.arange(18).reshape((3,6)) \n",
    "print(t1)\n",
    "print(t2) \n",
    "print(\"-\"*50)\n",
    "print(t1-t2) "
   ],
   "id": "6c2802a242e1e83e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0 1 2 3 4 5]]\n",
      "--------------------------------------------------\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:58:31.309689Z",
     "start_time": "2025-02-25T07:58:31.306389Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组尺寸不一样，其中一个数组的某一个维度的长度为1，可以进行计算\n",
    "t1 = np.arange(24).reshape((4,6)) \n",
    "t2 = np.arange(0,6).reshape((1,6)) \n",
    "print(t1)\n",
    "print(t2) \n",
    "print(\"-\"*50)\n",
    "print(t1-t2) "
   ],
   "id": "13a451523c92c0b1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0 1 2 3 4 5]]\n",
      "--------------------------------------------------\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:20:31.258374Z",
     "start_time": "2025-02-25T14:20:31.252446Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(4).reshape((4,1))\n",
    "print(t1)\n",
    "print(t2) \n",
    "print(\"-\"*50)\n",
    "print(t1-t2)"
   ],
   "id": "5ee28a58afd75b25",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:07:14.100577Z",
     "start_time": "2025-02-25T08:07:14.097638Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#6.3 练习轴\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "#按那个轴运算，哪个轴就会消失\n",
    "print(np.sum(a,axis=0))  # 0轴是行，1轴是列\n",
    "\n",
    "print(np.sum(a,axis=1)) \n",
    "\n",
    "print(np.sum(a))"
   ],
   "id": "c380a2f082f4414e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n",
      "21\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:24:56.077294Z",
     "start_time": "2025-02-25T14:24:56.071684Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24) \n",
    "print(t.shape)\n",
    "# 转换成二维\n",
    "a = t.reshape((2,3,4)) \n",
    "a #最外面的方括号是0轴，最内层的方括号是最大轴"
   ],
   "id": "432a51f49ed2b4d6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(24,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[12, 13, 14, 15],\n",
       "        [16, 17, 18, 19],\n",
       "        [20, 21, 22, 23]]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "ad77fa072012b29c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:25:42.044279Z",
     "start_time": "2025-02-25T14:25:42.038778Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b=np.sum(a, axis=0)\n",
    "print(\"-\"*20)\n",
    "print(f'b{b.shape}')\n",
    "print(b)\n",
    "c=np.sum(a, axis=1)\n",
    "print(\"-\"*20)\n",
    "print(f'c{c.shape}')\n",
    "print(c)\n",
    "d=np.sum(a, axis=2)\n",
    "print(\"-\"*20)\n",
    "print(f'd{d.shape}')\n",
    "print(d)"
   ],
   "id": "9321450d2e4e0323",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n",
      "b(3, 4)\n",
      "[[12 14 16 18]\n",
      " [20 22 24 26]\n",
      " [28 30 32 34]]\n",
      "--------------------\n",
      "c(2, 4)\n",
      "[[12 15 18 21]\n",
      " [48 51 54 57]]\n",
      "--------------------\n",
      "d(2, 3)\n",
      "[[ 6 22 38]\n",
      " [54 70 86]]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 7 索引和切片",
   "id": "2185e8c223075086"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:27:24.294110Z",
     "start_time": "2025-02-25T14:27:24.288549Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[2:4])\n",
    "\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2::2])"
   ],
   "id": "19d6a32d1d2bc750",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3]\n",
      "[2 4 6 8]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:27:56.842343Z",
     "start_time": "2025-02-25T14:27:56.837845Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print('*'*20)\n",
    "print(t1[1]) # 取一行(一行代表是一条数据，索引也是从0开始的) print(t1[1,:]) # 取一行\n",
    "print('*'*20)\n",
    "print(t1[1:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[1:3,:])# 取连续的多行\n",
    "print('*'*20)"
   ],
   "id": "124c90f991a741b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:27:57.465637Z",
     "start_time": "2025-02-25T14:27:57.459347Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(t1[[0,2,3]])# 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3],:])# 取不连续的多行,取所有列\n",
    "print('*'*20)\n",
    "print(t1[:,1])# 取一列\n",
    "print('*'*20)\n",
    "print(t1[:,1:])# 连续的多列\n",
    "print('*'*20)\n",
    "print(t1[:,[0,2,3]])# 取不连续的多列\n",
    "print('*'*20)\n",
    "print(t1[2,3])# # 取某一个值,三行四列\n",
    "print('*'*20)\n",
    "print(t1[2:4,3:5])# 取某一块区域\n",
    "print('*'*20)\n",
    "print(t1[[1,2],[0,2]])"
   ],
   "id": "c56e23e4b5a40a21",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 1  7 13 19]\n",
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "********************\n",
      "15\n",
      "********************\n",
      "[[15 16]\n",
      " [21 22]]\n",
      "********************\n",
      "[ 6 14]\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 8 数组的修改",
   "id": "a46d229cbc855051"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:42:06.500289Z",
     "start_time": "2025-02-25T08:42:06.497281Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "\n",
    "# # 修改某一行的值\n",
    "# t[1, :] = 0\n",
    "#\n",
    "# # 修改某一列的值\n",
    "# t[:, 1] = 0\n",
    "#\n",
    "# # 修改连续多行\n",
    "# t[1:3, :] = 0\n",
    "#\n",
    "# # 修改连续多列\n",
    "# t[:, 1:4] = 0\n",
    "#\n",
    "# # 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "# t[1:4,2:5]=0\n",
    "#\n",
    "# # 修改多个不相邻的点\n",
    "# t[[0,1],[2,3]]=0\n",
    "# print(t)\n",
    "# 可以根据条件修改，比如讲小于10的值改掉\n",
    "# t[t < 10] = 0\n",
    "# print(t)\n",
    "# 使用逻辑判断\n",
    "# np.logical_and   & # np.logical_or  |\n",
    "# np.logical_not      ~\n",
    "# t[(t > 2) & (t < 6)] = 0  # 与\n",
    "# t[(t < 2) | (t > 6)] = 0  # 或\n",
    "t[~(t > 6)] = 0  # 非\n",
    "print(t)\n"
   ],
   "id": "bcc7c075cdf67286",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:41:23.347861Z",
     "start_time": "2025-02-25T08:41:23.344861Z"
    }
   },
   "cell_type": "code",
   "source": "t<10",
   "id": "c081ba974bb1178c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:41:26.513040Z",
     "start_time": "2025-02-25T08:41:26.510537Z"
    }
   },
   "cell_type": "code",
   "source": "t[t < 10]",
   "id": "1f01363479c61a35",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:42:54.204985Z",
     "start_time": "2025-02-25T08:42:54.202643Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "t=t.clip(10,18) # 限制数组的最小值和最大值,小于10的改成10，大于18的改成18\n",
    "print(t)\n"
   ],
   "id": "f8bbb8b45e97fc28",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:43:55.879220Z",
     "start_time": "2025-02-25T08:43:55.876241Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# # 拓 展\n",
    "# # 三目运算（ np.where(condition, x, y)满足条件(condition)，输出x，不满足输出y。)）\n",
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "result = np.where( score > 80, True, False )\n",
    "print( result )"
   ],
   "id": "4b5086116426534c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 9 数组的添加，删除，去重",
   "id": "b51f4aad58f06423"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:30:50.961728Z",
     "start_time": "2025-02-25T14:30:50.956055Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print(' 向 数 组 添 加 元 素 ：')\n",
    "print(np.append(a, [7, 8, 9]))\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 0 添加元素：')\n",
    "print(np.append(a, [[7, 8, 9]], axis=0)) #轴是谁，谁就变\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 1 添加元素：')\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))\n"
   ],
   "id": "5f0736e78163692",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      " 向 数 组 添 加 元 素 ：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:31:33.103304Z",
     "start_time": "2025-02-25T14:31:33.097718Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. numpy.insert\n",
    "# 函数在给定索引之前，沿给定轴在输入数组中插入值。# 如果值的类型转换为要插入，则它与输入数组不同。\n",
    "# 插入没有原地的，函数会返回一个新数组。 此外，如果未提供轴，则输入数组会被展开。\n",
    "\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 3, [11, 12]))\n",
    "print('\\n')\n",
    "print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "\n",
    "print('沿轴  0 广播：')\n",
    "print(np.insert(a, 1, [11,12], axis=0))\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴  1 广播：')\n",
    "print(np.insert(a, 1, 11, axis=1))"
   ],
   "id": "e61107c4b0b473be",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "\n",
      "传递了 Axis 参数。 会广播值数组来配输入数组。\n",
      "沿轴  0 广播：\n",
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴  1 广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:50:16.842633Z",
     "start_time": "2025-02-25T08:50:16.838116Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(12).reshape(3,4)\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "print(np.delete(a,5))\n",
    "print('\\n')\n",
    "\n",
    "print('删除每一行中的第二列：')\n",
    "print(np.delete(a,1,axis = 1))\n",
    "print('\\n')"
   ],
   "id": "f1ee0420dd54412d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "\n",
      "删除每一行中的第二列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:32:48.587747Z",
     "start_time": "2025-02-25T14:32:48.582617Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([5,2,6,2,7,5,6,8,2,9])\n",
    "\n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "\n",
    "print ('第一个数组的去重值：')\n",
    "u = np.unique(a)\n",
    "print (u)\n",
    "print ('\\n')\n",
    "\n"
   ],
   "id": "2af8f21ec6056193",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "\n",
      "\n",
      "第一个数组的去重值：\n",
      "[2 5 6 7 8 9]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:33:06.209321Z",
     "start_time": "2025-02-25T14:33:06.203451Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print ('去重数组的索引数组：')\n",
    "u,indices = np.unique(a, return_index = True)\n",
    "print (indices)\n",
    "print ('\\n')\n",
    "\n",
    "print ('我们可以看到每个和原数组下标对应的数值：')\n",
    "print (a)\n",
    "print ('去重数组的下标：')\n",
    "u,indices = np.unique(a,return_inverse = True)\n",
    "print (u)\n",
    "print (indices)\n",
    "print ('\\n')\n",
    "\n",
    "print ('返回去重元素的重复数量：')\n",
    "u,indices = np.unique(a,return_counts = True)\n",
    "print (u)\n",
    "print (indices)"
   ],
   "id": "b2d5f112df891812",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去重数组的索引数组：\n",
      "[1 0 2 4 7 9]\n",
      "\n",
      "\n",
      "我们可以看到每个和原数组下标对应的数值：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去重数组的下标：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 0 3 1 2 4 0 5]\n",
      "\n",
      "\n",
      "返回去重元素的重复数量：\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10 数组的计算",
   "id": "e3c1246c80379577"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:33:43.996902Z",
     "start_time": "2025-02-25T14:33:43.991970Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score,axis=0)\n",
    "print(result)\n",
    "# 3. 获取最小值\n",
    "result = np.min(score,axis=1)\n",
    "print(result)\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score,axis=0)\n",
    "print(result)\n"
   ],
   "id": "55463294c2e877dd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n",
      "[82 88]\n",
      "[80 81 75]\n",
      "[75 81]\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T14:33:45.096238Z",
     "start_time": "2025-02-25T14:33:45.089821Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5. 数据的比较\n",
    "result = np.maximum( [-2, -1, 0, 1, 2], 0 )  # 第一个参数中的每一个数与第二个参数比较返回大的\n",
    "print(result)\n",
    "result = np.minimum( [-2, -1, 0, 1, 2], 0 )  # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(result)\n",
    "result = np.maximum( [5, -1, 0, 1, 2], [1, 2, 3, 4, 5] )\n",
    "print(result)\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "\n",
    "# 6. 求平均值\n",
    "result = np.mean(score) # 获取所有数据的平均值\n",
    "print(result)\n",
    "result = np.mean(score,axis=0) # 获取某一行或者某一列的平均值\n",
    "print(result)\n",
    "# 7. 求前缀和\n",
    "arr    = np.array([[1,2,3], [4,5,6]])\n",
    "print(arr)\n",
    "\n",
    "'''\n",
    "[1, 2, 3]------>   |1 |2 |3 |\n",
    "[4, 5, 6]------>   |5=1+4 |7=2+5 |9=3+6|\n",
    "'''\n",
    "print(arr.cumsum(0))\n",
    "\n",
    "'''\n",
    "[1, 2, 3]------>   |1 |2+1   |3+2+1 |\n",
    "[4, 5, 6]------>   |4 |4+5   |4+5+6 |\n",
    "'''\n",
    "print(arr.cumsum(1))\n"
   ],
   "id": "c3aa46d41c814a30",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 1 2]\n",
      "[-2 -1  0  0  0]\n",
      "[5 2 3 4 5]\n",
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T09:04:53.522941Z",
     "start_time": "2025-02-25T09:04:53.518436Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 8. argmin求最小值索引\n",
    "print(score)\n",
    "result = np.argmin(score,axis=0)\n",
    "res=np.min(score,axis=0) #这样我们就可以知道最小的81是第二排的，是从前往后遍历的\n",
    "print(result,res)\n",
    "\n",
    "# 9. 求每一列的标准差（这里是总体标准差）\n",
    "# 标准差是一组数据平均值分散程度的一种度量。一个较大的标准差，代表大部分数值和其平均值之间差异较大；\n",
    "# 一个较小的标准差，代表这些数据较接近平均值反应出数据的波动稳定情况，越大表示波动越大，越不稳定。\n",
    "result = np.std(score,axis=0)\n",
    "print(result)\n",
    "\n",
    "# 10. 极 值\n",
    "result = np.ptp(score,axis=None)#就是最大值和最小值的差\n",
    "print(result)"
   ],
   "id": "5b93d3bc3843ba5b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "[2 1] [75 81]\n",
      "[2.94392029 3.29983165]\n",
      "13\n"
     ]
    }
   ],
   "execution_count": 87
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
